/**
 * TotalController.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.swing;

import javax.swing.*;

import org.windu2b.jcaddie.model.*;

/**
 * @author windu.2b
 * 
 */
public class TotalController implements CoursesListener
{
	private Caddie	          caddie;


	private UserPreferences	  preferences;


	JComponent	              totalView;


	private CoursesController	coursesController;




	public TotalController( Caddie caddie, UserPreferences preferences )
	{
		this.caddie = caddie;
		this.preferences = preferences;
		totalView = new TotalPane( this );
		addCaddieListener();
	}




	/**
	 * 
	 */
	private void addCaddieListener()
	{
		caddie.addCoursesListener( this );
	}




	/**
	 * Returns the view associated with this controller.
	 */
	public JComponent getView()
	{
		return this.totalView;
	}




	/**
	 * @see org.windu2b.jcaddie.model.CoursesListener#pieceOfCoursesChanged(org.windu2b.jcaddie.model.CoursesEvent)
	 */
	public void pieceOfCoursesChanged( CoursesEvent ev )
	{
		display();
	}




	private void display()
	{
		float total = 0;

		for ( CaddiePieceOfProduct piece : caddie.getProduct() )
		{
			total += piece.getTotalPrice();
		}

		( ( TotalPane ) getView() ).setTotalValue( new Float( total )
		        .toString() );
	}
}